package com.mlz.controller;

import com.mlz.pojo.Emp;
import com.mlz.pojo.Result;
import com.mlz.service.EmpService;
import com.mlz.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        //调用业务层,登录功能
        Emp loginEmp = empService.login(emp);

        //判断: 登录用户是否存在
        if(loginEmp != null){
            //自定义信息 -> JWT中有效载荷
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", loginEmp.getId());
            map.put("username",loginEmp.getUsername());
            map.put("name",loginEmp.getName());

            //使用JWT工具类,生成身份令牌
            String token = JwtUtils.generateJwt(map);
            return Result.success(token);
        }
      return Result.error("用户名或者密码错误");
    }











}
